/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controlPlaylisted;

import guiPlaylisted.PlaylistedApp;

import dataPlaylisted.Playlist;
import dataPlaylisted.Music;
import dataPlaylisted.Styles;

import dataBasePlaylisted.getPlaylistListCommand;
import dataBasePlaylisted.setPlaylistListCommand;

import Request.RequestPlaylist;
import Response.ResponsePlaylist;
import java.io.FileNotFoundException;
import java.io.IOException;

import java.util.ArrayList;
import java.util.HashMap;

/**
 *
 * @author Administrador
 */
public class PlaylistController {
    
    private static PlaylistController instance;

    private MusicController m_musicCtrl;

    private UserController m_userCtrl;

    private ArrayList<Playlist> m_playlistList;

    private Styles m_styles;

    private HashMap<String, ControlPlaylistCommand> m_mapCommand;

    private int free_id;
    
    private PlaylistController() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_playlistList = new ArrayList<Playlist>();
        m_styles = new Styles();
        m_mapCommand = new HashMap();
        free_id = 1;
        this.initCommand();
    }

    public static PlaylistController getInstance() throws FileNotFoundException, IOException, ClassNotFoundException{
        instance = PlaylistedApp.getPlaylistCtrl();
        if(instance == null)
            instance = new PlaylistController();
        return instance;
    }

    public void setMusicCtrl() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_musicCtrl = MusicController.getInstance();
    }

    public void setUserCtrl() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_userCtrl = UserController.getInstance();
    }
    
    public ArrayList<Playlist> getPlaylistList(){
        return m_playlistList;
    }

    public void loadPlaylistList() throws ClassNotFoundException{

        RequestPlaylist req;
        ResponsePlaylist resp;

        req = new RequestPlaylist();
        resp = new ResponsePlaylist();

        req.setParameter("loadPlaylistList");

        this.doPost(req, resp);
        
        if(resp.getObjectList()!=null){
            m_playlistList = resp.getObjectList();
            free_id = m_playlistList.size() + 1;
            m_userCtrl.setPlaylistList();
        }
    }

    private void saveMusicList() throws ClassNotFoundException{

        RequestPlaylist req;
        ResponsePlaylist  resp;

        req = new RequestPlaylist ();
        resp = new ResponsePlaylist ();

        req.setObjectList(m_playlistList);

        req.setParameter("savePlaylistList");

        this.doPost(req, resp);
    }

    public void setPlaylistonList(Playlist _playlist) throws ClassNotFoundException{
        m_playlistList.add(_playlist);
        this.saveMusicList();
        m_userCtrl.setPlaylistList();
    }

    public Playlist makePlaylistbyStyle(String _style){

        Playlist _playlist;
        _playlist = new Playlist();
        _playlist.setId(free_id);
        free_id++;
        
        if(m_userCtrl.getUser() != null)
            _playlist.setUserId(m_userCtrl.getUser().getId());
        else
            _playlist.setUserId(0);

        ArrayList<Music> music_list;
        music_list = new ArrayList<Music>();
        
        music_list = m_musicCtrl.getMusicList();

        for (int i=0; i < music_list.size(); i++){
            if(music_list.get(i).getSyle().equals(_style))
                _playlist.setPlaylistElement(music_list.get(i));
        }

        _playlist.setMusicNum(_playlist.getPlaylist().size());

        return _playlist;
    }

    public Playlist makePlaylistbyCammelot(String _cammelot){
        Playlist _playlist;
        _playlist = new Playlist();
        _playlist.setId(free_id);
        free_id++;

        if(m_userCtrl.getUser() != null)
            _playlist.setUserId(m_userCtrl.getUser().getId());
        else
            _playlist.setUserId(0);

        ArrayList<Music> music_list;
        music_list = new ArrayList<Music>();

        music_list = m_musicCtrl.getMusicList();

        for (int i=0; i < music_list.size(); i++){
            if(music_list.get(i).getCammelot().equals(_cammelot))
                _playlist.setPlaylistElement(music_list.get(i));
        }

        _playlist.setMusicNum(_playlist.getPlaylist().size());

        return _playlist;

    }

    private void initCommand() throws FileNotFoundException, IOException, ClassNotFoundException{
        m_mapCommand.put("loadPlaylistList", new getPlaylistListCommand());
        m_mapCommand.put("savePlaylistList", new setPlaylistListCommand());
     }


     private void doPost(RequestPlaylist req, ResponsePlaylist resp) throws ClassNotFoundException{
        m_mapCommand.get(req.getParameter()).execute(req, resp);
     }

}
